package com.soar.recruitment.common.ui.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.soar.recruitment.common.ui.interfaces.OnLoadClickListener;
import com.soar.recruitment.common.ui.view.LoadFrameLayout;

import org.greenrobot.eventbus.EventBus;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import me.yokeyword.fragmentation.SupportFragment;


public abstract class SOARBaseFragment extends SupportFragment {

    protected View baseView;
    protected Activity mActivity;
    protected FragmentManager mFragmentManager;
    protected LoadFrameLayout mLoadFrameLayout;

    private Unbinder unbinder;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mActivity = getActivity();
        mFragmentManager = ((AppCompatActivity)mActivity).getSupportFragmentManager();
        baseView = inflater.inflate(initLayout(), null);
        return baseView;
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        init(savedInstanceState);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    private void init(Bundle savedInstanceState) {
        bindIOC();

        if(showLoading()) {
            //mLoadFrameLayout = ButterKnife.findById(baseView, R.id.load_root_view);
            mLoadFrameLayout.showLoadingView();
            mLoadFrameLayout.setOnLoadClickListener(new OnLoadClickListener() {
                @Override
                public void retryLoad() {
                    mLoadFrameLayout.showLoadingView();
                    onRetryLoading();
                }
            });
        }

        registEventBus();
        initVariable();
        initView(savedInstanceState);
        initData();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unBindIOC();
        unRegistEventBus();
    }

    protected String getResStr(int strId) {
        return getResources().getString(strId);
    }

    private void bindIOC() {
        unbinder = ButterKnife.bind(this, baseView);
    }

    private void unBindIOC() {
        if(unbinder != null) {
            unbinder.unbind();
        }
    }

    /**
     * 点击重新加载
     */
    protected void onRetryLoading() {

    }

    private void registEventBus() {
        if(useEventBus()) {
            EventBus.getDefault().register(this);
        }
    }

    private void unRegistEventBus() {
        if(useEventBus()) {
            if(EventBus.getDefault().isRegistered(this)){
                EventBus.getDefault().unregister(this);
            }
        }
    }

    /**
     * 是否使用 EventBus - 默认不处理
     * @return
     */
    protected boolean useEventBus() {
        return false;
    }

    protected boolean showLoading() {
        return false;
    }

    public abstract int initLayout();
    public abstract void initVariable();
    public abstract void initView(Bundle savedInstanceState);
    public abstract void initData();
}
